//
//  BIDTransitionViewController.m
//  HomeworkAssistant
//
//  Created by mal on 15/11/11.
//  Copyright © 2015年 mal. All rights reserved.
//

#import "BIDTransitionViewController.h"
#import "BIDSpinnerViewII.h"
#import "AppDelegate+StaticMethod.h"
#import "BIDMainViewController.h"
#import "BIDLoginViewController.h"

//登录
static NSString *loginURL = @"userAndroid!login.action";

@interface BIDTransitionViewController ()
{
    UIImageView *_bgView;
    BIDSpinnerViewII *_spinnerView;
}
@end

@implementation BIDTransitionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;
    _spinnerView = (BIDSpinnerViewII*)[[[NSBundle mainBundle] loadNibNamed:@"BIDSpinnerViewII" owner:self options:nil] lastObject];
    _spinnerView.center = self.view.center;
    _spinnerView.hintLabel.text = @"正在获取用户信息";
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    _bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
    UIImage *bgImg = nil;
    NSString *strImgPath = [[NSBundle mainBundle] pathForResource:@"launch2208" ofType:@"png"];
    bgImg = [UIImage imageWithContentsOfFile:strImgPath];
    [_bgView setImage:bgImg];
    [self.view addSubview:_bgView];
    [self.view addSubview:_spinnerView];
    [_spinnerView startAnimate];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UIViewController *vc = [self getRootViewController];
        dispatch_async(dispatch_get_main_queue(), ^{
            //[NSThread sleepForTimeInterval:3];
            [_spinnerView stopAnimate];
            [self.navigationController setViewControllers:@[vc]];
        });
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *根据登录状态来决定根视图的选择
 */
- (UIViewController *)getRootViewController
{
    UIViewController *vc = nil;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *strUsername = [userDefaults objectForKey:@"username"];
    NSString *strPassword = [userDefaults objectForKey:@"password"];
    if(strUsername.length>0 && strPassword.length>0)
    {
        NSString *strURL = @"";
        NSString *strPost = @"";
        //登录
        strURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], loginURL];
        strPost = [[NSString alloc] initWithFormat:@"userName=%@&password=%@", strUsername, strPassword];
        
        id obj = [BIDDataCommunication getDataFromNetBySyn:strURL postValue:strPost];
        if([obj isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *responseDic = (NSDictionary*)obj;
            NSNumber *resValue = responseDic[@"isSuccess"];
            if([resValue intValue] == 1)
            {
                //登录成功
                NSDictionary *subDic = responseDic[@"info"];
                //保存userId
                NSString *strUserId = subDic[@"id"];
                NSLog(@"userId>>>>>>>%@", strUserId);
                //
                [AppDelegate setUserId:strUserId];
                [AppDelegate setUserTrueName:subDic[@"trueName"]];
                [AppDelegate setUserName:subDic[@"userName"]];
                [AppDelegate setUserDepartmentId:subDic[@"departmentid"]];
                //
                vc = [[BIDMainViewController alloc] init];
            }
            else
            {
                vc = [[BIDLoginViewController alloc] initWithNibName:@"BIDLoginViewController" bundle:nil];
            }
        }
        else
        {
            vc = [[BIDLoginViewController alloc] initWithNibName:@"BIDLoginViewController" bundle:nil];
        }
    }
    else
    {
        vc = [[BIDLoginViewController alloc] initWithNibName:@"BIDLoginViewController" bundle:nil];
    }
    return vc;
}

@end
